package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.http.BusinessException;
import com.example.demo.common.http.RestResult;
import com.example.demo.model.SystemLog;
import com.example.demo.model.command.SystemLogSaveCommand;
import com.example.demo.model.command.SystemLogUpdateCommand;
import com.example.demo.model.converter.SystemLogConverter;
import com.example.demo.model.resp.SystemLogDetailResp;
import com.example.demo.model.resp.SystemLogPageResp;
import com.example.demo.service.SystemLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 系统日志表 前端控制器
 * </p>
 *
 * @author chenzl
 * @since 2023-10-11
 */
@RestController
@RequestMapping("/systemLog")
@Api(tags = "系统日志表")
public class SystemLogController {

    @Resource
    private SystemLogService systemLogService;

    @PostMapping("/save")
    @ApiOperation(value = "新增系统日志表")
    public RestResult<String> save(@RequestBody @Validated SystemLogSaveCommand command) {
        SystemLog systemLog = SystemLogConverter.INSTANCE.convert(command);
        systemLogService.save(systemLog);
        return RestResult.getSuccessResult(String.valueOf(systemLog.getId()));
    }

    @PostMapping("/removeById")
    @ApiOperation(value = "根据id删除系统日志表")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "String")
    )
    public RestResult<Boolean> removeById(String id) {
        boolean success = systemLogService.removeById(Long.parseLong(id));
        return RestResult.getSuccessResult(success);
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新系统日志表")
    public RestResult<Boolean> updateById(@RequestBody @Validated SystemLogUpdateCommand command) {
        SystemLog systemLog = SystemLogConverter.INSTANCE.convert(command);
        boolean success = systemLogService.updateById(systemLog);
        return RestResult.getSuccessResult(success);
    }

    @PostMapping("/getById")
    @ApiOperation(value = "根据id查询系统日志表")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "String")
    )
    public RestResult<SystemLogDetailResp> getById(String id) {
        SystemLog systemLog = systemLogService.getById(Long.parseLong(id));
        if (null == systemLog) {
            throw new BusinessException("id不存在");
        }
        SystemLogDetailResp resp = SystemLogConverter.INSTANCE.convert2DetailResp(systemLog);
        return RestResult.getSuccessResult(resp);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询系统日志表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "Integer", paramType = "query")
    })
    public RestResult<Page<SystemLogPageResp>> page(Integer pageNum, Integer pageSize) {
        Page<SystemLog> page = systemLogService.page(new Page<>(pageNum, pageSize));
        Page<SystemLogPageResp> resp = SystemLogConverter.INSTANCE.convert2PageResp(page);
        return RestResult.getSuccessResult(resp);
    }
}
